let express = require('express');
let mysql = require('./db');

let router = express.Router();

// 获取订单数据列表
router.get('/list', (req, res) => {
    let { page, size } = req.query
    // 当不传递 page 和 limit 需要设置默认值
    page = page || 1;
    size = size || 20;
    let index = (page - 1) * size;
    mysql.query(`select * from order1 order by Oid desc limit ${index},${size};`, (state, data) => {
        if (state) {
            mysql.query(`select count(*) as total from order1;`, (state1, total) => {
                if (state1) {
                    res.send({
                        code: 200,
                        msg: '请求成功',
                        total: total[0].total,
                        data
                    })
                } else {
                    res.send({
                        code: 400,
                        msg: '请求错误',
                        data
                    })
                }
            })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

// 根据用户名查询用户订单数据列表
router.get('/list/username', (req, res) => {
    let { username, page, size } = req.query;
    // 当不传递 page 和 limit 需要设置默认值
    page = page || 1;
    size = size || 20;
    let index = (page - 1) * size;
    mysql.query(`select CONVERT_TZ(complete_time,'+00:00','+08:00') as complete_time,Oid,order_num,telphone,username,complete,dishes,count_price from order1 where username='${username}' order by Oid desc limit ${index},${size};`, (state, data) => {
        if (state) {
            mysql.query(`select count(*) as total from order1 where username='${username}';`, (state1, total) => {
                if (state1) {
                    res.send({
                        code: 200,
                        msg: '数据查询成功',
                        total: total[0].total,
                        data
                    })
                } else {
                    res.send({
                        code: 400,
                        msg: '数据查询错误错误',
                        data
                    })
                }
            })
        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

// 查询用户订单数据列表
router.get('/list/:id', (req, res) => {
    let { Oid, page, size } = req.query;
    // 当不传递 page 和 limit 需要设置默认值
    page = page || 1;
    size = size || 20;
    let index = (page - 1) * size;
    mysql.query(`select * from order1 where Oid='${Oid}' limit ${index},${size};`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '数据查询成功',
                data:data[0]
            })

        } else {
            res.send({
                code: 400,
                msg: '请求错误',
                data
            })
        }
    })
})

//写入订单数据
router.post('/addData', (req, res) => {
    let { username, complete_time, telphone, dishes, count_price,complete } = req.body;
    complete = complete || 1;
   console.log('complete_time',complete_time);
    let time = complete_time.slice(0, 10).split('-').join('')
console.log('time',time);
    let order_num = time + telphone + Math.ceil(Math.random() * 10000 + 1)

    console.log(time);
    mysql.query(`insert into order1(Oid,order_num,telphone,username,complete_time,complete,dishes,count_price) values(Null,'${order_num}','${telphone}','${username}','${complete_time}','${complete}','${dishes}','${count_price}');`, (state) => {
        if (state) {
            res.send({
                code: 200,
                msg: '添加商品成功'
            })
        } else {
            res.send({
                code: 400,
                msg: '添加商品失败'
            })
        }
    })
})

//删除订单数据
router.post('/delData', (req, res) => {
    let { Oid } = req.body;
    mysql.query(`delete from  order1 where Oid='${Oid}';`, (state) => {
        if (state) {
            res.send({
                code: 200,
                msg: '删除订单成功'
            })
        } else {
            res.send({
                code: 400,
                msg: '删除订单失败'
            })
        }
    })
})

//批量删除订单数据
router.post('/delSome', function (req, res) {
    let { ids } = req.body
    // let dishes_ids =  ids.join(',')
    mysql.query(`delete from order1 where Oid in (${ids});`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '菜品移除成功',
                data
            })

        } else {
            res.send({
                code: 400,
                msg: '菜品移除失败',
                data
            })
        }
    })
})

//更新订单状态
router.post('/update', function (req, res) {
    let { complete, Oid } = req.body
    mysql.query(`update order1 set complete='${complete}' where Oid='${Oid}';`, (state, data) => {
        if (state) {
            res.send({
                code: 200,
                msg: '状态更新成功',
            })

        } else {
            res.send({
                code: 400,
                msg: '状态更新失败',
            })
        }
    })
})



module.exports = router